﻿// (c) Copyright Mikhail Dikov. All other rights reserved.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI;
using System.Xml.Serialization;
using System.ComponentModel;
using WebPartExtenders;

namespace WebPartExtenderToolkit
{

    [ToolboxData("<{0}:ResizableWebPart runat=server></{0}:ResizableWebPart>")]
    [XmlRoot(Namespace = "ResizableWebPart")]
    public class ResizableWebPart : ExtenderWebPartBase
    {

        private ResizableExtender extender;

        protected override void CreateChildControls()
        {            
            base.CreateChildControls();
            extender = new ResizableExtender();           
            Controls.Add(extender);
            ChildControlsCreated = true;
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            extender.TargetControlID = this.TargetControlID;
            extender.Enabled = Enabled && !string.IsNullOrEmpty(TargetControlID);
            extender.BehaviorID = this.BehaviorID;
            extender.ID = this.ID + "extender";
            extender.HandleCssClass = HandleCssClass;
            extender.ResizableCssClass = ResizableCssClass;
            extender.MinimumWidth = MinimumWidth;
            extender.MinimumHeight = MinimumHeight;
            extender.MaximumWidth = MaximumWidth;
            extender.MaximumHeight = MaximumHeight;
            extender.HandleOffsetX = HandleOffsetX;
            extender.HandleOffsetY = HandleOffsetY;
            //Always hide the extender control chrome, because it'll show up as an empty webpart
            ChromeType = PartChromeType.None;
            Title = "Resizable WebPart Extender";
        }

        #region ResizableControlExtender Properies

        [Category("Extender"),
        WebBrowsable(true), 
        WebDisplayName("HandleCssClass"),
        WebDescription("The name of the CSS class to apply to the resize handle."),
        Browsable(true), 
        Personalizable(PersonalizationScope.Shared),
        DisplayName("HandleCssClass"),
        Description("The name of the CSS class to apply to the resize handle.")]
        public string HandleCssClass{ get; set;}

        [Category("Extender"),
        WebBrowsable(true), 
        WebDisplayName("ResizableCssClass"),
        WebDescription("The name of the CSS class to apply to the element when resizing."),
        Browsable(true), 
        Personalizable(PersonalizationScope.Shared),
        DisplayName("ResizableCssClass"),
        Description("The name of the CSS class to apply to the element when resizing.")]
        public string ResizableCssClass{ get; set;}

        [Category("Extender"),
        WebBrowsable(true), 
        WebDisplayName("Minimum Width"),
        WebDescription("Minimum dimensions of the resizable element."),
        Browsable(true), 
        Personalizable(PersonalizationScope.Shared),
        DisplayName("Minimum Width"),
        Description("Minimum dimensions of the resizable element.")]
        public int MinimumWidth{ get; set;}

        [Category("Extender"), 
        WebBrowsable(true), 
        WebDisplayName("Minimum Height"),
        WebDescription("Minimum dimensions of the resizable element."),
        Browsable(true), 
        Personalizable(PersonalizationScope.Shared),
        DisplayName("Minimum Height"),
        Description("Minimum dimensions of the resizable element.")]
        public int MinimumHeight { get; set; }

        [Category("Extender"), 
        WebBrowsable(true),
        WebDisplayName("Maximum Width"),
        WebDescription("Maximum dimensions of the resizable element."),
        Browsable(true),
        Personalizable(PersonalizationScope.Shared),
        DisplayName("Maximum Width"),
        Description("Maximum dimensions of the resizable element.")]
        public int MaximumWidth{ get; set;}

        [Category("Extender"), 
        WebBrowsable(true), 
        WebDisplayName("Maximum Height"),
        WebDescription("Maximum dimensions of the resizable element."),
        Personalizable(PersonalizationScope.Shared),
        DisplayName("Maximum Height"),
        Description("Maximum dimensions of the resizable element.")]
        public int MaximumHeight{ get; set;}

        [Category("Extender"),
        WebBrowsable(true), 
        WebDisplayName("HandleOffsetX"),
        WebDescription("Offsets to apply to the location of the resize handle."),
        Browsable(true), 
        Personalizable(PersonalizationScope.Shared),
        DisplayName("HandleOffsetX"),
        Description("Offsets to apply to the location of the resize handle.")]
        public int HandleOffsetX { get; set; }

        [Category("Extender"),
        WebBrowsable(true), 
        WebDisplayName("HandleOffsetY"),
        WebDescription("Offsets to apply to the location of the resize handle."),
        Browsable(true), 
        Personalizable(PersonalizationScope.Shared),
        DisplayName("HandleOffsetY"),
        Description("Offsets to apply to the location of the resize handle.")]
        public int HandleOffsetY { get; set; }


	    #endregion

    }
}
